home *** CD-ROM | disk | FTP | other *** search
- #include <Controls.h>
- #include <Desk.h>
- #include <Dialogs.h>
- #include <DiskInit.h>
- #include <Errors.h>
- #include <Events.h>
- #include <Files.h>
- #include <Fonts.h>
- #include <Memory.h>
- #include <Menus.h>
- #include <OSEvents.h>
- #include <OSUtils.h>
- #include <Packages.h>
- #include <Palettes.h>
- #include <QuickDraw.h>
- #include <Resources.h>
- #include <Scrap.h>
- #include <Script.h>
- #include <SegLoad.h>
- #include <Signal.h>
- #include <StandardFile.h>
- #include <StdIO.h>
- #include <StdLib.h>
- #include <SysEqu.h>
- #include <TextEdit.h>
- #include <TextEdit.h>
- #include <ToolUtils.h>
- #include <Traps.h>
- #include <Types.h>
- #include <Values.h>
- #include <Windows.h>
-
-
- #include "ui.h"
- #include "camlmenus.h"
-
- #define rMenuBar 128 /* Liste des menus */
-
- #define mApple 128 /* Menu Pomme */
- #define iAbout 1 /* item 'A propos...' */
-
- #define mFile 129 /* Menu File */
- #define iOpen 1 /* item Open... =O */
- #define iClose 3 /* item Close =W */
- #define iSave 4 /* item Save =S */
- #define iInclude 6
- #define iCompile 7
- #define iLoad 8
- #define iLoadObject 9
- #define iQuit 11 /* item Quit =Q */
-
- #define mEdit 130 /* Menu Edit */
- #define iUndo 1 /* item Undo =Z */
- #define iCut 3 /* item Cut =X */
- #define iCopy 4 /* item Copy =C */
- #define iPaste 5 /* item Paste =V */
- #define iClear 6 /* item Clear */
- #define iSelAll 8 /* item Select All =A */
-
- #define mCaml 131 /* Menu History */
- #define iShowGraph 1 /* Show Graphic =G */
- #define iShowHist 2 /* Show History =H */
- #define iPrevEnt 3 /* Previous Entry =P */
- #define iNextEnt 4 /* Next Entry =N */
- #define iInterrupt 6 /* Interrupt Caml =. */
- #define iGC 7
- #define iPref 9 /* item Preferences */
-
- #define rErrorList 128 /* Liste des messages d'erreur */
- #define eNoMemory 1 /* Not enough memory */
- #define eNoRessource 2 /* The needed resource cannot be load */
- #define eNoTextEdit 3 /* TextEdit cannot be used */
- #define eNoScrap 4 /* The clipboard is unusable */
- #define eNoScrapPut 5 /* Impossible to write on the clipboard */
- #define eNoScrapGet 6 /* Impossible to read the clipborad */
- #define eNoMoreText 7 /* This window cannot get more memory */
- #define eWrongMachine 8 /* Caml Light cannot run on this type of Macintosh */
- #define eNoMoreHistory 9 /* No more room in the history */
- #define eTooMuchChars 10 /* Too much characters in this window */
- #define eCannotCreate 11 /* Cannot create the file. */
- #define eCannotWrite 12 /* Cannot write to the file. */
- #define eCannotReplace 13 /* Cannot replace the file. */
-
- #define rErrorAlert 128 /* alerte */
- #define rAboutAlert 129 /* alerte pour A propos... */
- #define rSizeAlert 130 /* alerte preferences texte */
-
- #define rPreferences 128 /* dialogue Preferences */
- #define iOK 1 /* item OK */
- #define iCancel 2 /* item Cancel */
- #define iDefault 3 /* item Default */
-
- #define iFirstControl 4 /* The first control is Return */
- #define iReturn 4 /* item Return = Send To Caml */
- #define iEnter 5 /* item Enter = Send To Caml */
- #define iShHistory 6 /* item Show History */
- #define iAutoScroll 7 /* item Auto scroll Output */
- #define iSavePos 8 /* item Save position of window */
- #define iPrMaxSize 9 /* item Printer Max Size */
- #define iPrFlushSize 10 /* item Printer Flush Size */
- #define iHiMaxSize 11 /* item History Max Size */
- #define iHiFlushSize 12 /* item History Flush Size */
- #define iTextCreator 13
- #define iLastControl 13 /* The last control is TextCreator */
-
- #define rPREF 128 /* resource preferences */
-
- #define rWindow 128 /* fenetre de donnees */
-
- #define rWTitle 129 /* Liste des noms de fenetres */
- #define tOutput 1 /* Nom fenetre Output */
- #define tInput 2 /* Nom fenetre Input */
- #define tHistory 3 /* Nom fenetre History */
- #define tGraphic 4 /* Nom fenetre Graphic */
-
- #define rMiscStr 130
- #define sSaveOutput 1
- #define sSaveHistory 2
- #define sUntitled 3
- #define sHistory_ml 4
-
- #define Visible 0xFF /* controle visible */
- #define inVisible 0 /* controle invisible */
- #define MARGINDRAG 4 /* marge limite pour drag */
- #define SBWIDTH 16 /* largeur scroll bar */
- #define WNDMARG 25 /* marge entre les fenetres */
- #define WMINSIZE 70 /* minimum taille d'une fenetre */
- #define WNORMWIDTH 500 /* largeur normale 80 colonnes */
- #define TXTMARG 4 /* marge d'une fenetre texte */
- #define WRAP 1 /* passage a la ligne sans CR */
- #define Enter 0x03 /* code ASCII touche Enter */
- #define CReturn 0x0D /* code ASCII touche Return */
- #define DelKey 0x7F /* code ASCII touche Del */
- #define TEWSIZE 200 /* taille fenetre text en chars */
- #define MAXCHAR 2000 /* maximum chars / ligne output */
- #define CHARWIDTH 6 /* largeur des caracteres */
- #define NSCHAR 5 /* nombre de chars a scroller */
- #define NSPIXEL 10 /* nombre de pixels a scroller */
- #define VSCROLLVIS 1 /* lignes visibles apres scroll */
- #define HSCROLLVIS 5 /* chars visibles apres scroll */
- #define TEBLOCK 1024 /* taille block memoire texte */
- #define BUFBLOCK 1024 /* taille block liste buffer */
- #define FONTSIZE 9 /* taille des caracteres */
- #define Ttop -1 /* souris au-dessus du texte */
- #define Tbottom -2 /* souris en-dessous du texte */
- #define Tleft -3 /* souris a gauche du texte */
- #define Tright -4 /* souris a droite du texte */
- #define Version 1 /* pour SysEnviron */
- #define nCR 2 /* nombre de cr entre buffers */
- #define MAXSIZE 50 /* taille max output par defaut */
- #define FLUSHSIZE 10 /* taille text purge par defaut */
- #define DIX 50 /* abscisse dialogue DiskEvt */
- #define DIY 50 /* ordonnee dialog DiskEvt */
- #define SIZEX 480 /* taille par defaut CamlGraph */
- #define SIZEY 280 /* taille par defaut CamlGraph */
-
- #define WindowType(window) ((((WindowPeek) (window))->windowKind == userKind) ? ((dataPeek) (window))->type : 0)
- #define isTEWindow(window) (WindowType(window) == tInput)
- #define isTextWindow(window) (((window) == CAMLPrinter) || ((window) == CAMLHistory))
- #define isGraphWindow(window) ((window) == CAMLGraph)
- #define ctl(i) (control[(i) - iFirstControl])
- #define gp ((graphPeek) CAMLOffScreen)
-
- typedef struct data *dataPeek;
-
- typedef struct data {
- WindowRecord window;
- ControlHandle vSBar;
- ControlHandle hSBar;
- short type; /* type de la fenetre : contient en fait l'index de son titre */
- long h; /* handle pour TEHandle par exemple... */
- dataPeek prevWindow;
- dataPeek nextWindow;
- } dataRecord;
-
- typedef struct LRect {
- long top;
- short left;
- short right;
- } LRect;
-
- typedef long **bufHandle;
-
- typedef struct text {
- LRect destRect;
- Rect viewRect;
- short lineHeight;
- short ascent;
- short width;
- long last_line_length;
- long selStart;
- long selEnd;
- long teLength;
- long firstLine;
- Handle hText;
- long nLines;
- bufHandle buffer;
- } textRecord, *textPtr, **textHandle;
-
- typedef struct graph {
- GrafPort offScreen;
- Rect destRect;
- Rect viewRect;
- Rect destRectZoom;
- } graphRecord, *graphPeek;
-
- typedef struct Preferences {
- long magic_number;
- Boolean Return; /* si true, Return envoie le texte a Caml */
- Boolean ShowHist;
- Boolean AutoScroll;
- Boolean SavePos;
- Rect LiWindowRect;
- Rect PrWindowRect;
- long PrMaxSize;
- long PrFlushSize;
- Rect HiWindowRect;
- long HiMaxSize;
- long HiFlushSize;
- Rect GrWindowRect;
- char text_creator [4];
- } *PrefPtr;
- typedef PrefPtr *PrefHandle;
-
- PrefPtr Pref = nil;
- int color_qd = 0;
- dataPeek WList; /* pointeur sur la premiere fenetre de la liste */
- Boolean inForeground;
- Boolean WNEAvailable;
- WindowPtr CAMLInput = nil;
- WindowPtr CAMLPrinter = nil;
- WindowPtr CAMLHistory = nil;
- WindowPtr CAMLGraph = nil;
- WindowPtr CAMLOffScreen = nil;
- GDHandle CAMLGDevice = nil;
- int max_depth;
- long indexBuffer;
- ClikLoopProcPtr oldClikLoop;
- int wait_graph_move = 0;
- #define N_graph_events 20
- EventRecord graph_event_queue [N_graph_events];
- int graph_event_head = 0;
- int graph_event_tail = 0;
- int reading = 0;
-
- RgnHandle FixCursor(void);
- void push_graph_event ();
- void DoEvent(EventRecord *event);
- void DoMouseDown(EventRecord *event);
- void DoClickContent(WindowPtr window, EventRecord *event);
- void SelectText(textHandle t, Point mouse, short extend);
- long MouseToText(textPtr t, Point mouse);
- pascal void DoClickTESBar(ControlHandle control, short part);
- pascal void DoClickTextSBar(ControlHandle control, short part);
- pascal void DoClickGraphSBar(ControlHandle control, short part);
- void AdjustMenus(WindowPtr window);
- void DoMenuCommand(long menuResult, WindowPtr window);
- void DoSave (WindowPtr window);
- void SendToHistory(TEHandle te);
- void GetPreferences(void);
- void SetDefaultPrefs(void);
- void GetWindowPos(WindowPtr window, Rect *r);
- void SetWindowPos(void);
- void SetPrefValues(ControlHandle *control);
- void SetPreferences(void);
- void UpdatePreferences(void);
- void CenterRect(Rect *r);
- void AdjustScrollBars(dataPeek doc);
- void SetScrollBar(ControlHandle control, short max, short value);
- void DoActivate(WindowPtr window, Boolean isActive);
- void DoUpdate(WindowPtr window);
- void TextUpdate(textHandle t);
- void InvertText(textHandle t, long start, long end);
- void FlushText(WindowPtr window);
- Boolean StoreChar(WindowPtr window, char c, Size max_size);
- void VScrollText(textHandle t, long dv);
- void HScrollText(textHandle t, short dh);
- void ScrollGraph(dataPeek doc, short dh, short dv);
- void DoGrowWindow(WindowPtr window, EventRecord *event);
- Boolean DoNew(short index, Rect *boundsRect);
- void OpenOutput(short windowType);
- void OpenInput(void);
- Boolean OpenGraph(void);
- void DoClose(WindowPtr window);
- pascal ProcPtr GetOldClikLoop(void);
- pascal void ClikLoop(void);
- void FixViewRect(WindowPtr window);
- void FixScrollBar(dataPeek doc);
- void Error(short error);
- void set_input (char *text, long len);
-
- extern pascal void AsmClikLoop(void);
-
- unsigned long DoRead(char *text, unsigned long len)
- {
- bufHandle buffer;
- long index, begin, maxLength;
- static long curPos = 0;
-
- buffer = (*((textHandle) ((dataPeek) CAMLHistory)->h))->buffer;
- while (true)
- if (((index = (*buffer)[1]) < **buffer) || (curPos > 0)) {
- begin = (*buffer)[index] + curPos;
- if (len > (maxLength = (*buffer)[index + 1] - begin - nCR + 1))
- len = maxLength;
- BlockMove((char *) (*(*((textHandle) ((dataPeek) CAMLHistory)->h))->hText + begin), text, len);
- if ((curPos += len) == (*buffer)[index + 1] - (*buffer)[index] - nCR + 1) {
- curPos = 0;
- (*buffer)[1]++;
- }
- DoWrite(CAMLPrinter, text, len);
- return len;
- }else{
- reading = 1;
- LookEvent(15);
- reading = 0;
- }
- }
-
- void DoWrite(WindowPtr window, char *text, long len)
- {
- GrafPtr port;
- long d, line, beg_str, end_str, invis, old_lines, first_line, old_length;
- char c;
- textHandle h;
- RgnHandle clipRgn;
- Rect r;
- Size max_size = (window == CAMLHistory) ? Pref->HiMaxSize : Pref->PrMaxSize;
- Size s;
- int drawn = 0;
-
- h = (textHandle) ((dataPeek) window)->h;
- #define t (*h)
- s = GetHandleSize (t->hText);
- GetPort(&port);
- SetPort(window);
- clipRgn = NewRgn();
- GetClip(clipRgn);
- r = t->viewRect;
- ClipRect(&r);
-
- first_line = t->firstLine;
- old_length = t->teLength;
- old_lines = t->nLines;
- line = t->last_line_length;
- end_str = 0;
- beg_str = 0;
- while (end_str < len){
- if (line >= MAXCHAR){
- c = '\n';
- invis = 0;
- }else{
- c = text [end_str];
- ++end_str;
- invis = 1;
- }
- if (s != 0){
- if (t->teLength >= s){
- if (s < max_size){
- SetHandleSize (t->hText, s + TEBLOCK);
- s = GetHandleSize (t->hText);
- }
- if (t->teLength >= s){
- FlushText (window);
- drawn = 1;
- }
- }
- /* if (t->teLength >= s) bug (); */
- (*t->hText) [t->teLength++] = c;
- }else{
- end_str -= invis;
- break;
- }
- if (c == '\n'){
- if (line * CHARWIDTH > t->destRect.right - t->destRect.left)
- t->destRect.right = t->destRect.left + line * CHARWIDTH;
- line = 0;
- ++t->nLines;
- beg_str = end_str;
- }else{
- ++line;
- }
- }
- if (line * CHARWIDTH > t->destRect.right - t->destRect.left)
- t->destRect.right = t->destRect.left + line * CHARWIDTH;
- if (!drawn){
- t->firstLine = old_length - t->last_line_length; /* old last line length */
- t->viewRect.top = t->destRect.top + t->lineHeight * (old_lines - 1);
- TextUpdate (h);
- t->firstLine = first_line;
- t->viewRect.top = r.top;
- }
-
- t->last_line_length = line;
-
- d = t->nLines - (t->viewRect.bottom - t->destRect.top) / t->lineHeight;
- if (d > 0
- && ((t->viewRect.bottom - t->destRect.top) / t->lineHeight >= old_lines
- || Pref->AutoScroll)){
- VScrollText(h, -d);
- }
-
- SetClip(clipRgn);
- DisposeRgn(clipRgn);
- AdjustScrollBars((dataPeek) window);
- SetPort(port);
- #undef t
- }
-
- void Initialize(void)
- {
- Handle menuBar;
- SysEnvRec Mac;
-
- if (GetApplLimit () > *(char **) CurStackBase - 40000){
- SetApplLimit (*(char **) CurStackBase - 40000);
- }
- MaxApplZone();
- InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- menuBar = GetNewMBar(rMenuBar);
- if (menuBar == nil) {
- Error(eNoMemory);
- ExitToShell();
- }
- SetMenuBar(menuBar);
- DisposHandle(menuBar);
- AddResMenu(GetMHandle(mApple), 'DRVR');
- DrawMenuBar();
- if ((SysEnvirons(Version, &Mac) != noErr) || (Mac.machineType < 0)) {
- Error(eWrongMachine);
- ExitToShell();
- }
- color_qd = Mac.hasColorQD;
- WNEAvailable = (NGetTrapAddress(_WaitNextEvent, ToolTrap) != GetTrapAddress(_Unimplemented));
- inForeground = true;
- WList = nil;
- GetPreferences ();
- SetWindowPos ();
- OpenInput();
- OpenOutput(tOutput);
- OpenOutput(tHistory);
- indexBuffer = 2;
- }
-
- void Quit(void)
- {
- if (Pref->SavePos) {
- if (CAMLInput != nil)
- GetWindowPos(CAMLInput, &Pref->LiWindowRect);
- if (CAMLPrinter != nil)
- GetWindowPos(CAMLPrinter, &Pref->PrWindowRect);
- if (CAMLHistory != nil)
- GetWindowPos(CAMLHistory, &Pref->HiWindowRect);
- if (CAMLGraph != nil)
- GetWindowPos (CAMLGraph, &Pref->GrWindowRect);
- UpdatePreferences();
- }
- while (WList != nil)
- DoClose((WindowPtr) WList);
- ExitToShell();
- }
-
- #define Incr(x) (x) = ((x) + 1) % N_graph_events
-
- void push_graph_event (void)
- {
- Incr (graph_event_head);
- if (graph_event_head == graph_event_tail) Incr (graph_event_tail);
- }
-
- void LookGraphEvent (EventRecord *result, int move_ok, int poll)
- {
- Point mouse_before, mouse_after;
- int i;
-
- if (poll){
- GetMouse (&result->where);
- LocalToGlobal (&result->where);
- result->modifiers = Button () ? 0 : btnState;
- result->what = nullEvent;
- for (i = graph_event_tail; i != graph_event_head; Incr (i)){
- if (graph_event_queue [i].what == keyDown){
- result->what = keyDown;
- result->message = graph_event_queue [i].message;
- break;
- }
- }
- return;
- }
- if (graph_event_head != graph_event_tail){
- *result = graph_event_queue [graph_event_tail];
- Incr (graph_event_tail);
- return;
- }
- GetMouse (&mouse_before);
- LocalToGlobal (&mouse_before);
- while (1){
- wait_graph_move = move_ok;
- LookEvent (15);
- GetMouse (&mouse_after);
- LocalToGlobal (&mouse_after);
- if (move_ok && mouse_after != mouse_before
- || graph_event_head != graph_event_tail)
- break;
- }
- if (graph_event_head == graph_event_tail){
- *result = graph_event_queue [graph_event_head];
- result->what = nullEvent;
- }else{
- *result = graph_event_queue [graph_event_tail];
- Incr (graph_event_tail);
- }
- }
-
- #undef Incr
-
- void LookEvent(unsigned long delay)
- {
- EventRecord event;
- Boolean eventToProcess;
- RgnHandle mouseRgn;
- WindowPtr window;
-
- do {
- mouseRgn = (inForeground) ? FixCursor() : nil;
- if (inForeground && isTEWindow(window = FrontWindow()))
- TEIdle((TEHandle) ((dataPeek) window)->h);
- if (WNEAvailable)
- eventToProcess = WaitNextEvent(everyEvent, &event, delay, mouseRgn);
- else {
- SystemTask();
- eventToProcess = GetNextEvent(everyEvent, &event);
- }
- graph_event_queue [graph_event_head] = event;
- if (mouseRgn != nil)
- DisposeRgn(mouseRgn);
- if (eventToProcess)
- DoEvent(&event);
- } while (eventToProcess);
- }
-
- RgnHandle FixCursor(void)
- {
- WindowPtr window;
- Point mouse;
- long h;
- Rect r;
- RgnHandle mouseRgn;
- RgnHandle screenRgn;
-
- window = FrontWindow();
- if (isTEWindow(window) || isTextWindow(window)) {
- SetPort(window);
- GetMouse(&mouse);
- h = ((dataPeek) window)->h;
- r = isTEWindow(window) ? (*((TEHandle) h))->viewRect : (*((textHandle) h))->viewRect;
- mouseRgn = NewRgn();
- RectRgn(mouseRgn, &r);
- OffsetRgn(mouseRgn, -window->portBits.bounds.left, -window->portBits.bounds.top);
- if (PtInRect(mouse, &r))
- SetCursor(*GetCursor(iBeamCursor));
- else {
- SetCursor(&qd.arrow);
- r = qd.screenBits.bounds;
- screenRgn = NewRgn();
- RectRgn(screenRgn, &r);
- DiffRgn(screenRgn, mouseRgn, mouseRgn);
- DisposeRgn(screenRgn);
- }
- return mouseRgn;
- }
- SetCursor(&qd.arrow);
- if (isGraphWindow (window) && wait_graph_move){
- wait_graph_move = 0;
- GetMouse (&mouse);
- mouseRgn = NewRgn ();
- SetRectRgn (mouseRgn, mouse.h, mouse.v, mouse.h + 1, mouse.v + 1);
- OffsetRgn (mouseRgn, -window->portBits.bounds.left, -window->portBits.bounds.top);
- return mouseRgn;
- }
- return nil;
- }
-
- void DoEvent(EventRecord *event)
- {
- char key;
- WindowPtr window;
- TEHandle te;
- Point thePoint;
-
- switch (event->what) {
- case mouseDown:
- DoMouseDown(event);
- break;
- case mouseUp:
- push_graph_event ();
- break;
- case keyDown:
- case autoKey:
- key = event->message & charCodeMask;
- window = FrontWindow();
- if (event->modifiers & cmdKey)
- if (key == CReturn)
- key = Enter;
- else {
- AdjustMenus(window);
- DoMenuCommand(MenuKey(key), window);
- return;
- }
- switch (WindowType(window)) {
- case tInput:
- te = (TEHandle) ((dataPeek) window)->h;
- if (Pref->Return && ((key == CReturn) || (key == Enter)))
- key = CReturn + Enter - key;
- if (key != Enter) {
- if (((*te)->teLength + (*te)->selEnd - (*te)->selStart + 1 < MAXSHORT)
- || (key == DelKey)
- || (key < ' '))
- TEKey(key, te);
- else
- Error(eTooMuchChars);
- } else {
- SendToHistory(te);
- if (event->modifiers & optionKey) {
- AdjustMenus(window);
- DoMenuCommand(MenuKey('N'), window);
- } else
- indexBuffer = **(*((textHandle) ((dataPeek) CAMLHistory)->h))->buffer;
- }
- AdjustScrollBars((dataPeek) window);
- break;
- case tOutput:
- case tHistory:
- SysBeep(1);
- break;
- case tGraphic:
- push_graph_event ();
- break;
- }
- break;
- case activateEvt:
- DoActivate((WindowPtr) event->message, (event->modifiers & activeFlag));
- break;
- case updateEvt:
- DoUpdate(window = (WindowPtr) event->message);
- break;
- case diskEvt:
- if (HiWord(event->message) != noErr) {
- SetPt(&thePoint, DIX, DIY);
- DIBadMount(thePoint, event->message);
- }
- break;
- case app4Evt:
- if (((event->message & osEvtMessageMask) >> 24) == suspendResumeMessage) {
- inForeground = (event->message & resumeFlag);
- DoActivate(FrontWindow(), inForeground);
- }
- break;
- }
- }
-
- void DoMouseDown(EventRecord *event)
- {
- WindowPtr window;
- short part;
- Rect r;
-
- part = FindWindow(event->where, &window);
- switch (part) {
- case inMenuBar:
- AdjustMenus(window = FrontWindow());
- DoMenuCommand(MenuSelect(event->where), window);
- break;
- case inSysWindow:
- SystemClick(event, window);
- break;
- case inContent:
- if (window != FrontWindow())
- SelectWindow(window);
- else
- DoClickContent(window, event);
- break;
- case inDrag:
- r = qd.screenBits.bounds;
- r.top += GetMBarHeight();
- InsetRect(&r, MARGINDRAG, MARGINDRAG);
- DragWindow(window, event->where, &r);
- break;
- case inGrow:
- DoGrowWindow(window, event);
- break;
- case inGoAway:
- HideWindow(window);
- break;
- case inZoomIn:
- case inZoomOut:
- if (TrackBox(window, event->where, part)) {
- SetPort(window);
- EraseRect(&window->portRect);
- ZoomWindow(window, part, window == FrontWindow());
- FixViewRect(window);
- if (isGraphWindow(window)) {
- switch (part) {
- case inZoomIn:
- gp->destRect = gp->destRectZoom;
- break;
- case inZoomOut:
- gp->destRectZoom = gp->destRect;
- gp->destRect = gp->viewRect;
- gp->destRect.right = gp->destRect.left + SIZEX;
- gp->destRect.top = gp->destRect.bottom - SIZEY;
- }
- ClipRect(&CAMLGraph->portRect);
- }
- InvalRect(&window->portRect);
- (*((dataPeek) window)->vSBar)->contrlVis = inVisible; /* pour que AdjustScrollBars ne redessine pas */
- (*((dataPeek) window)->hSBar)->contrlVis = inVisible; /* les controles, ce sera fait via le updateEvt */
- AdjustScrollBars((dataPeek) window);
- FixScrollBar((dataPeek) window);
- }
- break;
- }
- }
-
- void DoClickContent(WindowPtr window, EventRecord *event)
- {
- Point mouse;
- dataPeek doc;
- short max, scale;
- long realMax;
- ControlHandle control;
- TEHandle te;
- TEPtr pte;
- textHandle t;
- textPtr pt;
- graphPeek g;
-
- mouse = event->where;
- doc = (dataPeek) window;
- SetPort(window);
- GlobalToLocal(&mouse);
- switch (WindowType(window)) {
- case tInput:
- te = (TEHandle) doc->h;
- switch (FindControl(mouse, window, &control)) {
- case 0:
- if (PtInRect(mouse, &(*te)->viewRect)) {
- TEClick(mouse, ((event->modifiers & shiftKey) != 0), te);
- AdjustScrollBars(doc);
- }
- break;
- case inThumb:
- pte = *te;
- if (control == doc->vSBar) {
- max = pte->nLines - (pte->viewRect.bottom - pte->viewRect.top) / pte->lineHeight; /* il faut avoir le max reel... */
- max += (pte->teLength) && (*(*pte->hText + pte->teLength - 1) == '\n');
- } else
- max = TEWSIZE - (pte->viewRect.right - pte->viewRect.left + CHARWIDTH - 1) / CHARWIDTH;
- if (max < GetCtlValue(control)) {
- if (max < 1)
- max = 1;
- (*control)->contrlValue = max;
- (*control)->contrlMax = max;
- }
- if (TrackControl(control, mouse, nil)) {
- pte = *te;
- if (control == doc->vSBar)
- TEScroll(0, pte->viewRect.top - pte->destRect.top - GetCtlValue(control) * pte->lineHeight, te);
- else
- TEScroll(pte->viewRect.left - pte->destRect.left - GetCtlValue(control) * CHARWIDTH, 0, te);
- }
- AdjustScrollBars(doc);
- break;
- default:
- TrackControl(control, mouse, (ProcPtr) DoClickTESBar);
- break;
- }
- break;
- case tOutput:
- case tHistory:
- t = (textHandle) doc->h;
- switch (FindControl(mouse, window, &control)) {
- case 0:
- if (PtInRect(mouse, &(*t)->viewRect))
- SelectText(t, mouse, event->modifiers & shiftKey);
- break;
- case inThumb:
- pt = *t;
- if (control == doc->vSBar) {
- realMax = pt->nLines - (pt->viewRect.bottom - pt->viewRect.top) / pt->lineHeight;
- scale = 1 + realMax / MAXSHORT;
- max = realMax / scale;
- } else
- max = (pt->destRect.right - pt->destRect.left - pt->viewRect.right + pt->viewRect.left + pt->width - 1) / pt->width;
- if (max < GetCtlValue(control)) {
- if (max < 1)
- max = 1;
- (*control)->contrlValue = max;
- (*control)->contrlMax = max;
- }
- if (TrackControl(control, mouse, nil)) {
- pt = *t;
- if (control == doc->vSBar)
- VScrollText(t, (pt->viewRect.top - pt->destRect.top) / pt->lineHeight - GetCtlValue(control) * scale);
- else
- HScrollText(t, (pt->viewRect.left - pt->destRect.left) / pt->width - GetCtlValue(control));
- }
- AdjustScrollBars(doc);
- break;
- default:
- TrackControl(control, mouse, (ProcPtr) DoClickTextSBar);
- break;
- }
- break;
- case tGraphic:
- switch (FindControl(mouse, window, &control)) {
- case 0:
- push_graph_event ();
- break;
- case inThumb:
- if (TrackControl(control, mouse, nil)) {
- g = (graphPeek) doc->h;
- if (control == doc->vSBar)
- ScrollGraph(doc, 0, g->viewRect.top - g->destRect.top - GetCtlValue(control));
- else
- ScrollGraph(doc, g->viewRect.left - g->destRect.left - GetCtlValue(control), 0);
- }
- break;
- default:
- TrackControl(control, mouse, (ProcPtr) DoClickGraphSBar);
- break;
- }
- break;
- }
- }
-
- void SelectText(textHandle t, Point mouse, short extend)
- {
- long org, pos, oldpos;
- char *text;
-
- oldpos = MouseToText(*t, mouse);
- if (extend) {
- pos = ((*t)->selStart + (*t)->selEnd) / 2;
- if (oldpos < pos) {
- org = (*t)->selEnd;
- InvertText(t, oldpos, (*t)->selStart);
- (*t)->selStart = oldpos;
- } else {
- org = (*t)->selStart;
- InvertText(t, (*t)->selEnd, oldpos);
- (*t)->selEnd = oldpos;
- }
- } else {
- InvertText(t, (*t)->selStart, (*t)->selEnd);
- (*t)->selStart = oldpos;
- (*t)->selEnd = oldpos;
- org = oldpos;
- }
- while (StillDown()) {
- GetMouse(&mouse);
- pos = MouseToText(*t, mouse);
- if (pos < 0) {
- switch (pos) {
- case Ttop:
- if ((*t)->viewRect.top > (*t)->destRect.top)
- VScrollText(t, 1);
- pos = (*t)->firstLine;
- break;
- case Tbottom:
- if (((*t)->viewRect.bottom - (*t)->destRect.top) / (*t)->lineHeight < (*t)->nLines)
- VScrollText(t, -1);
- SetPt(&mouse, (*t)->viewRect.right - 1, (*t)->viewRect.bottom - 1);
- for (pos = MouseToText(*t, mouse), text = *(*t)->hText; (pos < (*t)->teLength) && (*(text + pos) != '\n'); pos++)
- ;
- if (*(text + pos) == '\n')
- pos++;
- break;
- case Tleft:
- if ((*t)->viewRect.left > (*t)->destRect.left)
- HScrollText(t, 1);
- mouse.h = (*t)->viewRect.left;
- pos = MouseToText(*t, mouse);
- break;
- case Tright:
- if ((*t)->viewRect.right < (*t)->destRect.right)
- HScrollText(t, -1);
- mouse.h = (*t)->viewRect.right - 1;
- pos = MouseToText(*t, mouse);
- break;
- }
- AdjustScrollBars((dataPeek) FrontWindow());
- }
- if (pos != oldpos) {
- InvertText(t, pos, oldpos);
- (*t)->selStart = (pos >= org) ? org : pos;
- (*t)->selEnd = (pos >= org) ? pos : org;
- oldpos = pos;
- }
- }
- }
-
- long MouseToText(textPtr t, Point mouse)
- {
- long i;
- short nlines, n;
- char *text;
-
- if (mouse.v < t->viewRect.top)
- return Ttop;
- if (mouse.v >= t->viewRect.bottom)
- return Tbottom;
- if (mouse.h < t->viewRect.left - t->width / 2)
- return Tleft;
- if (mouse.h >= t->viewRect.right)
- return Tright;
- nlines = (t->viewRect.bottom - t->viewRect.top) / t->lineHeight;
- i = t->firstLine;
- text = *t->hText;
- for (n = 1; (i < t->teLength) && (n <= nlines) && (mouse.v >= t->viewRect.top + n * t->lineHeight); i++, n++)
- while ((i < t->teLength) && (*(text + i) != '\n'))
- i++;
- if ((i < t->teLength) && (mouse.v < t->viewRect.top + n * t->lineHeight)) {
- for (n = (t->viewRect.left - t->destRect.left) / t->width; (i < t->teLength) && (n > 0) && (*(text + i) != '\n'); i++, n--)
- ;
- if (n == 0)
- while ((i < t->teLength) && (mouse.h >= t->viewRect.left + n * t->width + t->width / 2) && (*(text + i) != '\n'))
- i++, n++;
- return i;
- }
- return t->teLength;
- }
-
- pascal void DoClickTESBar(ControlHandle control, short part)
- {
- dataPeek doc;
- TEPtr te;
- short value, d;
-
- if (part) {
- doc = (dataPeek) (*control)->contrlOwner;
- value = GetCtlValue(control);
- te = *((TEHandle) doc->h);
- switch (part) {
- case inUpButton:
- case inDownButton:
- d = (control == doc->vSBar) ? 1 : NSCHAR;
- break;
- case inPageUp:
- case inPageDown:
- if (control == doc->vSBar)
- d = (te->viewRect.bottom - te->viewRect.top) / te->lineHeight - VSCROLLVIS;
- else
- d = (te->viewRect.right - te->viewRect.left) / CHARWIDTH - HSCROLLVIS;
- break;
- }
- if ((part == inDownButton) || (part == inPageDown))
- d = -d;
- if (d > value)
- d = value;
- else
- if (value - d > GetCtlMax(control))
- d = value - GetCtlMax(control);
- if (control == doc->vSBar)
- TEScroll(0, d * te->lineHeight, (TEHandle) doc->h);
- else {
- d *= CHARWIDTH;
- if (part == inUpButton)
- d += (te->viewRect.left - te->destRect.left) % CHARWIDTH;
- TEScroll(d, 0, (TEHandle) doc->h);
- }
- AdjustScrollBars(doc); /* les scroll bars peuvent devenir inactives, ce */
- } /* que ne permet pas un simple SetCtlValue. */
- }
-
- pascal void DoClickTextSBar(ControlHandle control, short part)
- {
- dataPeek doc;
- textPtr t;
- short d;
- long max, value;
-
- if (part) {
- doc = (dataPeek) (*control)->contrlOwner;
- t = *((textHandle) doc->h);
- if (control == doc->vSBar) {
- max = t->nLines - (t->viewRect.bottom - t->viewRect.top) / t->lineHeight; /* il faut les vraies valeurs, sans passer */
- value = (t->viewRect.top - t->destRect.top) / t->lineHeight; /* par le facteur d'echelle... */
- if (max < value)
- max = value;
- } else {
- max = GetCtlMax(control);
- value = GetCtlValue(control);
- }
- switch (part) {
- case inUpButton:
- case inDownButton:
- d = (control == doc->vSBar) ? 1 : NSCHAR;
- break;
- case inPageUp:
- case inPageDown:
- if (control == doc->vSBar)
- d = (t->viewRect.bottom - t->viewRect.top) / t->lineHeight - VSCROLLVIS;
- else
- d = (t->viewRect.right - t->viewRect.left) / t->width - HSCROLLVIS;
- break;
- }
- if ((part == inDownButton) || (part == inPageDown))
- d = -d;
- if (d > value)
- d = value;
- else
- if (value - d > max)
- d = - ((short) (max - value));
- if (control == doc->vSBar)
- VScrollText((textHandle) doc->h, d);
- else
- HScrollText((textHandle) doc->h, d);
- AdjustScrollBars(doc);
- }
- }
-
- pascal void DoClickGraphSBar(ControlHandle control, short part)
- {
- dataPeek doc;
- graphPeek g;
- short d, value;
-
- if (part) {
- doc = (dataPeek) (*control)->contrlOwner;
- g = (graphPeek) doc->h;
- value = GetCtlValue(control);
- switch (part) {
- case inUpButton:
- case inDownButton:
- d = NSPIXEL;
- break;
- case inPageUp:
- case inPageDown:
- if (control == doc->vSBar)
- d = g->viewRect.bottom - g->viewRect.top;
- else
- d = g->viewRect.right - g->viewRect.left;
- d -= NSPIXEL;
- break;
- }
- if ((part == inDownButton) || (part == inPageDown))
- d = -d;
- if (d > value)
- d = value;
- else
- if (value - d > GetCtlMax(control))
- d = value - GetCtlMax(control);
- if (control == doc->vSBar)
- ScrollGraph(doc, 0, d);
- else
- ScrollGraph(doc, d, 0);
- }
- }
-
- void AdjustMenus(WindowPtr window)
- {
- MenuHandle hFile, hEdit, hCaml;
- TEPtr te;
- textPtr t;
- Boolean selAll, selection, paste, CutClear, prevEnt, nextEnt, save;
-
- if (isTEWindow(window)) {
- te = *((TEHandle) ((dataPeek) window)->h);
- selAll = (te->teLength > 0);
- selection = (te->selStart < te->selEnd);
- paste = (TEGetScrapLen() > 0);
- CutClear = selection;
- prevEnt = (indexBuffer > 2);
- nextEnt = (indexBuffer < **(*((textHandle) ((dataPeek) CAMLHistory)->h))->buffer);
- save = false;
- } else {
- if (isTextWindow(window)) {
- t = *((textHandle) ((dataPeek) window)->h);
- selAll = (t->teLength > 0);
- selection = (t->selStart < t->selEnd);
- save = true;
- } else {
- selAll = false;
- selection = false;
- save = false;
- }
- paste = false;
- CutClear = false;
- prevEnt = false;
- nextEnt = false;
- }
- hFile = GetMHandle (mFile);
- hEdit = GetMHandle(mEdit);
- hCaml = GetMHandle(mCaml);
- if (selAll)
- EnableItem(hEdit, iSelAll);
- else
- DisableItem(hEdit, iSelAll);
- if (selection)
- EnableItem(hEdit, iCopy);
- else
- DisableItem(hEdit, iCopy);
- if (paste)
- EnableItem(hEdit, iPaste);
- else
- DisableItem(hEdit, iPaste);
- if (CutClear) {
- EnableItem(hEdit, iCut);
- EnableItem(hEdit, iClear);
- } else {
- DisableItem(hEdit, iCut);
- DisableItem(hEdit, iClear);
- }
- if (prevEnt)
- EnableItem(hCaml, iPrevEnt);
- else
- DisableItem(hCaml, iPrevEnt);
- if (nextEnt)
- EnableItem(hCaml, iNextEnt);
- else
- DisableItem(hCaml, iNextEnt);
- if (isGraphWindow(window) || (window == CAMLHistory))
- EnableItem(hFile, iClose);
- else
- DisableItem(hFile, iClose);
- if (save){
- EnableItem (hFile, iSave);
- }else{
- DisableItem (hFile, iSave);
- }
- if (reading){
- EnableItem (hFile, iInclude);
- EnableItem (hFile, iCompile);
- EnableItem (hFile, iLoad);
- EnableItem (hFile, iLoadObject);
- EnableItem (hCaml, iGC);
- }else{
- DisableItem (hFile, iInclude);
- DisableItem (hFile, iCompile);
- DisableItem (hFile, iLoad);
- DisableItem (hFile, iLoadObject);
- DisableItem (hCaml, iGC);
- }
- }
-
- void set_input (char *text, long len)
- {
- Rect r;
- TEHandle te = (TEHandle) ((dataPeek) CAMLInput)->h;
-
- SetPort (CAMLInput);
- TEDeactivate(te);
- TESetSelect(0, (*te)->teLength, te);
- TEDelete(te);
- TEActivate(te);
- if (len > 0) {
- TESetText (text, len, te);
- r = CAMLInput->portRect;
- r.right -= SBWIDTH - 1;
- r.bottom -= SBWIDTH - 1;
- EraseRect(&r);
- TEUpdate(&r, te);
- }
- AdjustScrollBars((dataPeek) CAMLInput);
- }
-
- void DoMenuCommand(long menuResult, WindowPtr window)
- {
- short menuID, item, d;
- Str255 DAName;
- Handle h;
- textHandle t;
- TEHandle te;
- bufHandle buffer;
-
- if (menuID = HiWord(menuResult)) {
- item = LoWord(menuResult);
- switch (menuID) {
- case mApple:
- switch (item) {
- case iAbout:
- h = GetResource('ALRT', rAboutAlert);
- CenterRect((Rect *) *h);
- Alert(rAboutAlert, nil);
- break;
- default:
- GetItem(GetMHandle(mApple), item, DAName);
- OpenDeskAcc(DAName);
- }
- break;
- case mFile:
- switch (item) {
- case iOpen:
- break;
- case iClose:
- if (((WindowPeek) window)->windowKind < 0)
- CloseDeskAcc(-((WindowPeek) window)->windowKind);
- else
- HideWindow(window);
- break;
- case iSave:
- if (((WindowPeek) window)->windowKind < 0){
- /* Desk Accessory. Do nothing. */
- }else{
- DoSave (window);
- }
- break;
- case iQuit:
- Quit();
- break;
- case iInclude:
- do_include ();
- break;
- case iCompile:
- do_compile ();
- break;
- case iLoad:
- do_load ();
- break;
- case iLoadObject:
- do_load_object ();
- break;
- }
- break;
- case mEdit:
- if (!SystemEdit(item - 1))
- switch (item) {
- case iUndo:
- break;
- case iCut:
- TECut((TEHandle) ((dataPeek) window)->h);
- AdjustScrollBars((dataPeek) window);
- break;
- case iCopy:
- if (isTEWindow(window))
- TECopy((TEHandle) ((dataPeek) window)->h);
- else {
- t = (textHandle) ((dataPeek) window)->h;
- if ((*t)->selStart < (*t)->selEnd)
- if (ZeroScrap() != noErr)
- Error(eNoScrap);
- else {
- HLock((*t)->hText);
- if (PutScrap((*t)->selEnd - (*t)->selStart, 'TEXT', *(*t)->hText + (*t)->selStart) != noErr)
- Error(eNoScrapPut);
- HUnlock((*t)->hText);
- }
- }
- break;
- case iPaste:
- te = (TEHandle) ((dataPeek) window)->h;
- if (TEGetScrapLen() + (*te)->teLength + (*te)->selStart - (*te)->selEnd < MAXSHORT) {
- TEPaste(te);
- AdjustScrollBars((dataPeek) window);
- } else
- Error(eTooMuchChars);
- break;
- case iClear:
- TEDelete((TEHandle) ((dataPeek) window)->h);
- AdjustScrollBars((dataPeek) window);
- break;
- case iSelAll:
- if (isTEWindow(window)) {
- te = (TEHandle) ((dataPeek) window)->h;
- TESetSelect(0, (*te)->teLength, te);
- } else {
- t = (textHandle) ((dataPeek) window)->h;
- InvertText(t, 0, (*t)->selStart);
- InvertText(t, (*t)->selEnd, (*t)->teLength);
- (*t)->selStart = 0;
- (*t)->selEnd = (*t)->teLength;
- }
- break;
- }
- break;
- case mCaml:
- switch (item) {
- case iInterrupt:
- HiliteMenu(0);
- raise(SIGINT);
- break;
- case iShowHist:
- ShowWindow(CAMLHistory);
- SelectWindow(CAMLHistory);
- break;
- case iPrevEnt:
- case iNextEnt:
- t = (textHandle) ((dataPeek) CAMLHistory)->h;
- buffer = (*t)->buffer;
- d = 0;
- if ((item == iPrevEnt) && (indexBuffer > 2))
- d = -1;
- if ((item == iNextEnt) && (indexBuffer < **buffer))
- d = 1;
- if (d != 0) {
- indexBuffer += d;
- HLock ((*t)->hText);
- if (indexBuffer < **buffer){
- set_input (*(*t)->hText + (*buffer) [indexBuffer],
- (*buffer) [indexBuffer + 1] - (*buffer)[indexBuffer] - nCR);
- }else{
- set_input (nil, 0);
- }
- }
- break;
- case iShowGraph:
- ShowWindow(CAMLGraph);
- SelectWindow(CAMLGraph);
- break;
- case iPref:
- SetPreferences();
- break;
- case iGC:
- do_gc ();
- break;
- }
- break;
- }
- HiliteMenu(0);
- }
- }
-
- void DoSave (WindowPtr window)
- {
- Handle text_handle = (*(textHandle) (((dataPeek) window)->h))->hText;
- DialogTHndl the_dialog;
- Point there;
- Str255 prompt, default_name;
- SFReply reply;
- short file_ref_num;
- short result_code;
- Rect sb = qd.screenBits.bounds;
- long count, creator;
-
- switch (WindowType (window)){
- case tOutput:
- GetIndString (prompt, rMiscStr, sSaveOutput);
- GetIndString (default_name, rMiscStr, sUntitled);
- break;
- case tHistory:
- GetIndString (prompt, rMiscStr, sSaveHistory);
- GetIndString (default_name, rMiscStr, sHistory_ml);
- break;
- default:
- return;
- }
- the_dialog = (DialogTHndl) GetResource ('DLOG', putDlgID);
- if (the_dialog != nil && *the_dialog != nil){
- Rect *r = &(*the_dialog)->boundsRect;
-
- CenterRect (r);
- there.h = r->left;
- there.v = r->top;
- }else{
- there.h = 200;
- there.v = 100;
- }
- SFPutFile (there, prompt, default_name, NULL, &reply);
- if (!reply.good) return;
- result_code = FSDelete (reply.fName, reply.vRefNum);
- if (result_code != noErr && result_code != fnfErr){
- Error (eCannotReplace);
- return;
- }
- memcpy (&creator, Pref->text_creator, 4);
- result_code = Create (reply.fName, reply.vRefNum, creator, 'TEXT');
- if (result_code != noErr){
- Error (eCannotCreate);
- return;
- }
- if (FSOpen (reply.fName, reply.vRefNum, &file_ref_num) != noErr){
- if (result_code == noErr) FSDelete (reply.fName, reply.vRefNum);
- Error (eCannotWrite);
- return;
- }
- count = (*(textHandle) (((dataPeek) window)->h))->teLength;
- if (FSWrite (file_ref_num, &count, *text_handle) != noErr){
- FSClose (file_ref_num);
- if (result_code == noErr) FSDelete (reply.fName, reply.vRefNum);
- Error (eCannotWrite);
- return;
- }
- FSClose (file_ref_num);
- return;
- }
-
- void SendToHistory(TEHandle te)
- {
- bufHandle buffer;
- Size s;
-
- if ((*te)->teLength > 0) {
- buffer = (*((textHandle) ((dataPeek) CAMLHistory)->h))->buffer;
- if (((**buffer + 2) * sizeof(long)) > (s = GetHandleSize((Handle) buffer))) {
- SetHandleSize((Handle) buffer, s + BUFBLOCK);
- if (MemError() != noErr) {
- Error(eNoMoreHistory);
- return;
- }
- }
- HLock((*te)->hText);
- DoWrite(CAMLHistory, (char *) *(*te)->hText, (*te)->teLength);
- HUnlock((*te)->hText);
- DoWrite(CAMLHistory, "\n\n", nCR); /* le nombre de '\n' est egal a nCR */
- (*buffer)[++**buffer] = (*((textHandle) ((dataPeek) CAMLHistory)->h))->teLength;
- TEDeactivate(te);
- TESetSelect(0, (*te)->teLength, te);
- TEDelete(te);
- TEActivate(te);
- }
- }
-
- void GetPreferences(void)
- {
- PrefHandle resource;
-
- resource = (PrefHandle) GetResource ('PREF', rPREF);
- if (resource == nil){
- resource = (PrefHandle) NewHandle (sizeof (struct Preferences));
- AddResource ((Handle) resource, 'PREF', rPREF, "\p");
- (*resource)->magic_number = 'WRNG';
- }
- if (resource == nil){
- Error (eNoMemory);
- ExitToShell ();
- }
- if (GetHandleSize ((Handle) resource) != sizeof (struct Preferences))
- SetHandleSize ((Handle) resource, sizeof (struct Preferences));
- HLock ((Handle) resource);
- Pref = *resource;
- if (Pref->magic_number != 'CAML') SetDefaultPrefs ();
- }
-
- void SetDefaultPrefs (void)
- {
- Pref->magic_number = 'CAML';
- Pref->Return = true;
- Pref->ShowHist = false;
- Pref->AutoScroll = true;
- Pref->SavePos = false;
- Pref->PrMaxSize = MAXSIZE * 1024;
- Pref->PrFlushSize = FLUSHSIZE * 1024;
- Pref->HiMaxSize = MAXSIZE / 2 * 1024;
- Pref->HiFlushSize = FLUSHSIZE / 2 * 1024;
- memcpy (Pref->text_creator, "ttxt", 4);
- }
-
- void GetWindowPos(WindowPtr window, Rect *r)
- {
- Point *p = (Point *) r;
-
- SetPort (window);
- *r = window->portRect;
- LocalToGlobal (&p[0]);
- LocalToGlobal (&p[1]);
- }
-
- void fit_in_screen (Rect *w, Rect *screen)
- {
- Rect temp;
-
- if (w->bottom - w->top > screen->bottom - screen->top){
- w->bottom = w->top + screen->bottom - screen->top;
- }
- if (!SectRect (w, screen, &temp)){
- w->bottom = screen->top + w->bottom - w->top;
- w->top = screen->top;
- w->right = screen->left + w->right - w->left;
- w->left = screen->left;
- }
- if (w->top < screen->top){
- w->bottom += screen->top - w->top;
- w->top = screen->top;
- }
- }
-
- void send_to_caml (char *text)
- {
- set_input (text, strlen (text));
- SendToHistory ((TEHandle) ((dataPeek) CAMLInput)->h);
- indexBuffer = **(*((textHandle) ((dataPeek) CAMLHistory)->h))->buffer;
- }
-
- void SetWindowPos(void)
- {
- Rect screen;
-
- screen = qd.screenBits.bounds;
- screen.top += GetMBarHeight() + SBWIDTH;
- InsetRect(&screen, MARGINDRAG, MARGINDRAG);
- if (Pref->SavePos){
- /* Only change windows that are completely off-screen
- or too big. */
- fit_in_screen (&Pref->PrWindowRect, &screen);
- fit_in_screen (&Pref->LiWindowRect, &screen);
- fit_in_screen (&Pref->HiWindowRect, &screen);
- fit_in_screen (&Pref->GrWindowRect, &screen);
- }else{
- SetRect (&Pref->LiWindowRect,
- screen.left, screen.bottom - WMINSIZE,
- screen.left + WNORMWIDTH, screen.bottom);
- fit_in_screen (&Pref->LiWindowRect, &screen);
- SetRect (&Pref->PrWindowRect,
- screen.left, screen.top,
- screen.left + WNORMWIDTH, Pref->LiWindowRect.top - WNDMARG);
- fit_in_screen (&Pref->PrWindowRect, &screen);
- SetRect (&Pref->HiWindowRect,
- Pref->PrWindowRect.right + MARGINDRAG, screen.top,
- Pref->PrWindowRect.right + MARGINDRAG + WNORMWIDTH, screen.bottom);
- if (Pref->HiWindowRect.right > screen.right) Pref->HiWindowRect.right = screen.right;
- if (Pref->HiWindowRect.right - Pref->HiWindowRect.left < WMINSIZE){
- Pref->HiWindowRect.left = Pref->HiWindowRect.right - WMINSIZE;
- }
- fit_in_screen (&Pref->HiWindowRect, &screen);
- SetRect (&Pref->GrWindowRect,
- screen.left, screen.top,
- screen.left + SIZEX + SBWIDTH -1, screen.top + SIZEY + SBWIDTH -1);
- fit_in_screen (&Pref->GrWindowRect, &screen);
- }
- }
-
- void SetPrefValues(ControlHandle *control)
- {
- short item;
- Str255 text;
- long size[4];
-
- SetCtlValue(ctl(iReturn), Pref->Return);
- SetCtlValue(ctl(iEnter), 1 - Pref->Return);
- SetCtlValue(ctl(iShHistory), Pref->ShowHist);
- SetCtlValue(ctl(iAutoScroll), Pref->AutoScroll);
- SetCtlValue(ctl(iSavePos), Pref->SavePos);
- size[0] = Pref->PrMaxSize;
- size[1] = Pref->PrFlushSize;
- size[2] = Pref->HiMaxSize;
- size[3] = Pref->HiFlushSize;
- for (item = iPrMaxSize; item <= iHiFlushSize; item++) {
- NumToString(size[item - iPrMaxSize] / 1024, text);
- SetIText((Handle) ctl(item), text);
- }
- text [0] = 4;
- memcpy (&text [1], Pref->text_creator, 4);
- SetIText ((Handle) ctl (iTextCreator), text);
- }
-
- void SetPreferences(void)
- {
- DialogPtr dialog;
- short item, i;
- long size[4];
- Handle resHandle, itemHandle;
- ControlHandle control[iLastControl - iFirstControl + 1];
- Rect r;
- Str255 text;
-
- if ((resHandle = GetResource('DLOG', rPreferences)) == nil)
- Error(eNoRessource);
- else {
- CenterRect((Rect *) *resHandle);
- dialog = GetNewDialog(rPreferences, nil, (WindowPtr) (-1));
- for (item = iFirstControl; item <= iLastControl; item ++)
- GetDItem(dialog, item, &i, &((Handle) ctl(item)), &r);
- SetPrefValues(control);
- GetDItem(dialog, iOK, &i, &itemHandle, &r);
- DoActivate(FrontWindow(), false);
- SetPort(dialog);
- PenSize(3,3);
- InsetRect(&r, -4, -4);
- ShowWindow(dialog);
- FrameRoundRect(&r, 16, 16);
- do {
- ModalDialog(nil, &item);
- switch (item) {
- case iOK:
- for(i = iPrMaxSize; i <= iHiFlushSize; i++) {
- GetIText((Handle) ctl(i), text);
- StringToNum(text, &size[i - iPrMaxSize]);
- if ((size[i - iPrMaxSize] *= 1024) <= 0)
- break;
- }
- if ((i > iHiFlushSize) && (i = iPrFlushSize) && (size[1] < size[0]) && (i = iHiFlushSize) && (size[3] < size[2])) {
- Pref->Return = GetCtlValue(ctl(iReturn));
- Pref->ShowHist = GetCtlValue(ctl(iShHistory));
- Pref->AutoScroll = GetCtlValue(ctl(iAutoScroll));
- Pref->SavePos = GetCtlValue(ctl(iSavePos));
- Pref->PrMaxSize = size[0];
- Pref->PrFlushSize = size[1];
- Pref->HiMaxSize = size[2];
- Pref->HiFlushSize = size[3];
- GetIText ((Handle) ctl (iTextCreator), text);
- for (i = 0; i < 4; i++){
- if (i < text [0]){
- Pref->text_creator [i] = text [i+1];
- }else{
- Pref->text_creator [i] = ' ';
- }
- }
- } else {
- itemHandle = GetResource('ALRT', rSizeAlert);
- CenterRect((Rect *) *itemHandle);
- StopAlert(rSizeAlert, nil);
- SelIText(dialog, i, 0, 32767);
- item = iCancel + 1;
- }
- break;
- case iDefault:
- SetDefaultPrefs ();
- SetPrefValues(control);
- break;
- case iReturn:
- case iEnter:
- SetCtlValue(ctl(iReturn), (item == iReturn));
- SetCtlValue(ctl(iEnter), (item == iEnter));
- break;
- case iShHistory:
- SetCtlValue(ctl(iShHistory), 1 - GetCtlValue(ctl(iShHistory)));
- break;
- case iAutoScroll:
- SetCtlValue(ctl(iAutoScroll), 1 - GetCtlValue(ctl(iAutoScroll)));
- break;
- case iSavePos:
- SetCtlValue(ctl(iSavePos), 1 - GetCtlValue(ctl(iSavePos)));
- break;
- default:
- break;
- }
- } while (item > iCancel);
- if (item == iOK)
- UpdatePreferences();
- DisposeDialog(dialog);
- ReleaseResource(resHandle);
- }
- }
-
- void UpdatePreferences(void)
- {
- ChangedResource (RecoverHandle ((Ptr) Pref));
- }
-
- void CenterRect(Rect *r)
- {
- short d;
- Rect *bounds;
-
- bounds = &qd.screenBits.bounds;
- d = r->right - r->left;
- r->left = bounds->left + (bounds->right - bounds->left - d) / 2;
- r->right = r->left + d;
- d = r->bottom - r->top;
- r->top = bounds->top + (bounds->bottom - bounds->top - d) / 3;
- r->bottom = r->top + d;
- }
-
- void AdjustScrollBars(dataPeek doc)
- {
- TEPtr te;
- textPtr t;
- short max, value, scale;
- long x;
-
- switch (WindowType((WindowPtr) doc)) {
- case tInput:
- te = *((TEHandle) doc->h);
- max = te->nLines - (te->viewRect.bottom - te->viewRect.top) / te->lineHeight;
- max += (te->teLength) && (*(*te->hText + te->teLength - 1) == '\n');
- value = (te->viewRect.top - te->destRect.top) / te->lineHeight;
- SetScrollBar(doc->vSBar, max, value);
- te = *((TEHandle) doc->h); /* le TERecord peut avoir change de place... */
- max = TEWSIZE - (te->viewRect.right - te->viewRect.left + CHARWIDTH - 1) / CHARWIDTH;
- value = (te->viewRect.left - te->destRect.left) / CHARWIDTH;
- break;
- case tOutput:
- case tHistory:
- t = *((textHandle) doc->h);
- x = t->nLines - (t->viewRect.bottom - t->viewRect.top) / t->lineHeight;
- scale = 1 + x / MAXSHORT;
- max = x / scale;
- value = (t->viewRect.top - t->destRect.top) / t->lineHeight / scale;
- SetScrollBar(doc->vSBar, max, value);
- t = *((textHandle) doc->h);
- max = (t->destRect.right - t->destRect.left - t->viewRect.right + t->viewRect.left + t->width - 1) / t->width;
- value = (t->viewRect.left - t->destRect.left) / t->width;
- break;
- case tGraphic:
- max = gp->destRect.bottom - gp->destRect.top - gp->viewRect.bottom + gp->viewRect.top;
- value = gp->viewRect.top - gp->destRect.top;
- SetScrollBar(doc->vSBar, max, value);
- max = gp->destRect.right - gp->destRect.left - gp->viewRect.right + gp->viewRect.left;
- value = gp->viewRect.left - gp->destRect.left;
- }
- SetScrollBar(doc->hSBar, max, value);
- }
-
- void SetScrollBar(ControlHandle control, short max, short value)
- {
- unsigned char state;
-
- if (max < value)
- max = value;
- if ((max != GetCtlMax(control)) || (value != GetCtlValue(control))) {
- state = (*control)->contrlVis;
- (*control)->contrlVis = inVisible; /* empeche SetCtl... de dessiner la scroll bar */
- SetCtlMax(control, max); /* pour pouvoir le faire seulement sur demande */
- SetCtlValue(control, value); /* (ca sert pour DoGrowWindow...) */
- if (state == Visible)
- ShowControl(control);
- }
- }
-
- void draw_grow_icon (WindowPtr window)
- {
- PenState saved_pen;
-
- SetPort (window);
- GetPenState (&saved_pen);
- PenNormal ();
- DrawGrowIcon (window);
- SetPenState (&saved_pen);
- }
-
- void DoActivate(WindowPtr window, Boolean isActive)
- {
- dataPeek doc;
- Rect r;
- TEHandle te;
- textHandle t;
- RgnHandle clipRgn, tempRgn;
-
- doc = (dataPeek) window;
- if (isActive) {
- SetPort(window);
- if (isTextWindow(window)) {
- clipRgn = NewRgn();
- tempRgn = NewRgn();
- GetClip(clipRgn);
- CopyRgn(((WindowPeek) window)->updateRgn, tempRgn);
- OffsetRgn(tempRgn, window->portBits.bounds.left, window->portBits.bounds.top);
- DiffRgn(clipRgn, tempRgn, tempRgn);
- SetClip(tempRgn);
- DisposeRgn(tempRgn);
- t = (textHandle) doc->h;
- if ((*t)->selStart < (*t)->selEnd)
- InvertText(t, (*t)->selStart, (*t)->selEnd);
- SetClip(clipRgn);
- DisposeRgn(clipRgn);
- }
- if (isTEWindow(window)) {
- TEActivate(te = (TEHandle) doc->h);
- TEFromScrap();
- }
- (*doc->vSBar)->contrlVis = Visible;
- (*doc->hSBar)->contrlVis = Visible;
- r = (*doc->vSBar)->contrlRect;
- InvalRect(&r); /* un updateEvt redessinera les controles */
- r = window->portRect;
- r.top = r.bottom - SBWIDTH + 1;
- InvalRect(&r);
- } else {
- HideControl(doc->vSBar);
- HideControl(doc->hSBar);
- draw_grow_icon(window);
- SetPort(window);
- ValidRect(&(*doc->vSBar)->contrlRect); /* permet d'eviter l'updateEvt genere par HideControl */
- ValidRect(&(*doc->hSBar)->contrlRect);
- if (isTEWindow(window)) {
- if (TEGetScrapLen() > 0)
- if (ZeroScrap() != noErr)
- Error(eNoScrap);
- else
- if (TEToScrap() != noErr)
- Error(eNoScrapPut);
- TEDeactivate((TEHandle) doc->h);
- }
- if (isTextWindow(window)) {
- t = (textHandle) doc->h;
- if ((*t)->selStart < (*t)->selEnd)
- InvertText(t, (*t)->selStart, (*t)->selEnd);
- }
- }
- }
-
- void copy_bits (const BitMap *srcBits, const BitMap *dstBits, const Rect *srcRect,
- const Rect *dstRect, short mode, RgnHandle maskRgn)
- {
- if (color_qd){
- RGBColor fore, back;
- RGBColor white = {0xFFFF, 0xFFFF, 0xFFFF};
- RGBColor black = {0, 0, 0};
-
- GetForeColor (&fore);
- GetBackColor (&back);
- RGBForeColor (&black);
- RGBBackColor (&white);
- CopyBits (srcBits, dstBits, srcRect, dstRect, mode, maskRgn);
- RGBForeColor (&fore);
- RGBBackColor (&back);
- }else{
- CopyBits (srcBits, dstBits, srcRect, dstRect, mode, maskRgn);
- }
- }
-
- void copy_mask (const BitMap *srcBits, const BitMap *maskBits, const BitMap *dstBits,
- const Rect *srcRect, const Rect *maskRect, const Rect *dstRect)
- {
- if (color_qd){
- RGBColor fore, back;
- RGBColor white = {0xFFFF, 0xFFFF, 0xFFFF};
- RGBColor black = {0, 0, 0};
-
- GetForeColor (&fore);
- GetBackColor (&back);
- RGBForeColor (&black);
- RGBBackColor (&white);
- CopyMask (srcBits, maskBits, dstBits, srcRect, maskRect, dstRect);
- RGBForeColor (&fore);
- RGBBackColor (&back);
- }else{
- CopyMask (srcBits, maskBits, dstBits, srcRect, maskRect, dstRect);
- }
- }
-
- void DoUpdate(WindowPtr window)
- {
- Rect r;
-
- BeginUpdate(window);
- SetPort(window);
- EraseRect(&window->portRect);
- UpdateControls(window, window->visRgn);
- draw_grow_icon(window);
- r = window->portRect;
- r.right -= SBWIDTH - 1;
- r.bottom -= SBWIDTH - 1;
- if (SectRect(&r, &(*window->visRgn)->rgnBBox, &r))
- switch (WindowType(window)) {
- case tOutput:
- case tHistory:
- TextUpdate((textHandle) ((dataPeek) window)->h);
- break;
- case tInput:
- TEUpdate(&r, (TEHandle) ((dataPeek) window)->h);
- break;
- case tGraphic:
- { Rect offRect = r;
-
- OffsetRect (&offRect, gp->viewRect.left - gp->destRect.left,
- gp->viewRect.top - gp->destRect.top);
- copy_bits (&CAMLOffScreen->portBits, &window->portBits,
- &offRect, &r, srcCopy, nil);
- }
- }
- EndUpdate(window);
- }
-
- void TextUpdate(textHandle t) /* la procedure suppose que SetPort a deja ete fait... */
- {
- RgnHandle clipRgn, viewRgn;
- Rect r;
- long i;
- short nlines, nchars, firstChar, line, len;
- char *text;
- GrafPtr port;
-
- clipRgn = NewRgn();
- viewRgn = NewRgn();
- GetClip(clipRgn);
- r = (*t)->viewRect;
- RectRgn(viewRgn, &r);
- SectRgn(clipRgn, viewRgn, viewRgn); /* pour ne pas ecrire sur la scroll bar par exemple */
- SetClip(viewRgn);
- DisposeRgn(viewRgn);
- HLock((*t)->hText);
- nlines = (r.bottom - r.top) / (*t)->lineHeight;
- nchars = (r.right - r.left + (*t)->width - 1) / (*t)->width;
- firstChar = (r.left - (*t)->destRect.left) / (*t)->width;
- for (i = (*t)->firstLine, line = 0; (i < (*t)->teLength) && (line < nlines); i++, line++) {
- for (text = *(*t)->hText + i, len = 0; (i < (*t)->teLength) && (*(text + len) != '\n'); i++, len++)
- ;
- if (len > firstChar) {
- len -= firstChar;
- if (len > nchars)
- len = nchars;
- MoveTo(r.left, r.top + line * (*t)->lineHeight + (*t)->ascent);
- DrawText(text, firstChar, len);
- }
- }
- HUnlock((*t)->hText);
- GetPort(&port);
- if ((port == FrontWindow()) && inForeground)
- InvertText(t, (*t)->selStart, (*t)->selEnd);
- SetClip(clipRgn);
- DisposeRgn(clipRgn);
- }
-
- void InvertText(textHandle t, long start, long end)
- {
- long i;
- short nlines, firstChar, nchars, line, n, x;
- char *text;
- Rect r;
- RgnHandle selection;
-
- if ((i = start) > end) {
- start = end;
- end = i;
- }
- if ((end > (i = (*t)->firstLine)) && (start < (*t)->teLength)) {
- if (start < i)
- start = i;
- nlines = ((*t)->viewRect.bottom - (*t)->viewRect.top) / (*t)->lineHeight;
- firstChar = ((*t)->viewRect.left - (*t)->destRect.left) / (*t)->width;
- nchars = ((*t)->viewRect.right - (*t)->viewRect.left + (*t)->width - 1) / (*t)->width;
- text = *(*t)->hText + i;
- for (line = 0, x = 0; (i < (*t)->teLength) && (i < start) && (line < nlines); i++, x++)
- if (*text++ == '\n') {
- line++;
- x = -1;
- }
- if (line < nlines) {
- selection = NewRgn();
- OpenRgn();
- while ((i < (*t)->teLength) && (i < end) && (line < nlines)) {
- for (n = 0; (i < (*t)->teLength) && (i < end) && (*text != '\n'); i++, n++, text++)
- ;
- if ((i < (*t)->teLength) && (i < end) && (*text == '\n')) {
- n = firstChar + nchars;
- i++;
- text++;
- }
- if (x < firstChar + nchars) {
- if (x < firstChar) {
- n -= firstChar - x;
- x = firstChar;
- }
- if (n > 0) {
- if (x + n > firstChar + nchars)
- n = firstChar + nchars - x;
- if ((r.right = (r.left = (*t)->destRect.left + x * (*t)->width) + n * (*t)->width) > (*t)->viewRect.right)
- r.right = (*t)->viewRect.right;
- r.bottom = (r.top = (*t)->viewRect.top + line * (*t)->lineHeight) + (*t)->lineHeight;
- FrameRect(&r);
- }
- }
- x = 0;
- line ++;
- }
- CloseRgn(selection);
- InvertRgn(selection);
- DisposeRgn(selection);
- }
- }
- }
-
- void FlushText(WindowPtr window)
- {
- textPtr t;
- char *src, *text;
- long delta, i, j, *buffer;
- Boolean redraw = false;
- Rect r;
-
- t = *((textHandle) ((dataPeek) window)->h);
- delta = (window == CAMLPrinter) ? Pref->PrFlushSize : Pref->HiFlushSize;
- if (delta > t->teLength)
- delta = t->teLength;
- if (window == CAMLHistory) { /* aligne delta sur un buffer, et les decale */
- buffer = *t->buffer;
- if (delta > buffer[buffer[1]])
- delta = buffer[buffer[1]];
- for(i = 2; buffer[i] < delta; i++)
- ;
- delta = buffer[i];
- buffer[1] -= (i - 2);
- buffer[0] -= (i - 2);
- for(j = 2; j <= buffer[0]; i++, j++)
- buffer[j] = buffer[i] - delta;
- indexBuffer -= (i - 2);
- if (indexBuffer < 2) indexBuffer = buffer [0];
- }
- src = (text = *t->hText) + delta;
- for(i = (t->teLength -= delta), j = 1; i > 0; i--) { /* deplace le texte en comptant le nombre de lignes */
- *text = *src++;
- if (*text++ == '\n')
- j++;
- }
- if ((t->teLength > 0) && (*--text == '\n'))
- j--;
- if ((t->selStart -= delta) < 0) /* remise a jour de differentes variables */
- t->selStart = 0;
- if ((t->selEnd -= delta) < 0)
- t->selEnd = 0;
- if ((t->firstLine -= delta) < 0) {
- t->firstLine = 0;
- redraw = true;
- }
- if ((t->destRect.top += (t->nLines - j) * t->lineHeight) > t->viewRect.top) {
- t->destRect.top = t->viewRect.top;
- t->firstLine = 0;
- redraw = true;
- }
- t->nLines = j;
- if (redraw) {
- r = t->viewRect;
- EraseRect(&r);
- TextUpdate((textHandle) ((dataPeek) window)->h);
- }
- AdjustScrollBars((dataPeek) window);
- }
-
- void VScrollText(textHandle t, long dv)
- {
- long i, firstLine, line;
- short nlines;
- RgnHandle updateRgn;
- Rect r;
- char *text;
-
- (*t)->destRect.top += dv * (*t)->lineHeight;
- r = (*t)->viewRect;
- nlines = (r.bottom - r.top) / (*t)->lineHeight;
- if (dv < 0) {
- for (i = (*t)->firstLine, text = *(*t)->hText, line = dv; (line < 0) && (i < (*t)->teLength); line++, i++)
- while ((i < (*t)->teLength) && (*(text + i) != '\n'))
- i++;
- (*t)->firstLine = i;
- if (dv <= -nlines) {
- EraseRect(&r);
- TextUpdate(t);
- } else {
- updateRgn = NewRgn();
- ScrollRect(&r, 0, dv * (*t)->lineHeight, updateRgn);
- if ((*updateRgn)->rgnBBox.top - r.bottom < dv * (*t)->lineHeight)
- dv = -((r.bottom - (*updateRgn)->rgnBBox.top) / (*t)->lineHeight + 1);
- for (firstLine = i, text = *(*t)->hText, line = nlines + dv; (i < (*t)->teLength) && (line > 0); i++)
- if (*(text + i) == '\n')
- line--;
- if (i < (*t)->teLength) {
- (*t)->firstLine = i;
- (*t)->viewRect.top += (nlines + dv) * (*t)->lineHeight;
- TextUpdate(t);
- (*t)->viewRect.top = r.top;
- (*t)->firstLine = firstLine;
- }
- DisposeRgn(updateRgn);
- }
- } else
- if ((dv > 0) && ((*t)->firstLine > 0)) {
- for (i = (*t)->firstLine - 1, text = *(*t)->hText, line = dv; (line >= 0) && (i >= 0); line--, i--)
- while ((i >= 0) && (*(text + i) != '\n'))
- i--;
- (*t)->firstLine = i + 2;
- if (dv >= nlines) {
- EraseRect(&r);
- TextUpdate(t);
- } else {
- updateRgn = NewRgn();
- ScrollRect(&r, 0, dv * (*t)->lineHeight, updateRgn);
- DisposeRgn(updateRgn);
- (*t)->viewRect.bottom = r.top + dv * (*t)->lineHeight;
- TextUpdate(t);
- (*t)->viewRect.bottom = r.bottom;
- }
- }
- }
-
- void HScrollText(textHandle t, short dh)
- {
- Rect r;
- short nchars, bound;
- RgnHandle updateRgn, clipRgn;
-
- if (dh != 0) {
- (*t)->destRect.left += dh * (*t)->width;
- (*t)->destRect.right += dh * (*t)->width;
- r = (*t)->viewRect;
- nchars = (r.right - r.left) / (*t)->width;
- if ((dh >= nchars) || (dh <= -nchars)) {
- EraseRect(&r);
- TextUpdate(t);
- } else {
- updateRgn = NewRgn();
- ScrollRect(&r, dh * (*t)->width, 0, updateRgn);
- clipRgn = NewRgn();
- GetClip(clipRgn);
- SetClip(updateRgn);
- if (dh > 0) {
- (*t)->viewRect.right = r.left + dh * (*t)->width;
- if ((*t)->viewRect.right < (bound = (*updateRgn)->rgnBBox.right))
- (*t)->viewRect.right = bound;
- } else {
- (*t)->viewRect.left += (nchars + dh) * (*t)->width;
- if ((*t)->viewRect.left > (bound = (*updateRgn)->rgnBBox.left))
- (*t)->viewRect.left = r.left + (bound - r.left) / (*t)->width * (*t)->width;
- }
- DisposeRgn(updateRgn);
- TextUpdate(t);
- (*t)->viewRect = r;
- SetClip(clipRgn);
- DisposeRgn(clipRgn);
- }
- }
- }
-
- void ScrollGraph(dataPeek doc, short dh, short dv)
- {
- RgnHandle updateRgn;
- Rect r, offRect;
-
- updateRgn = NewRgn();
- ScrollRect(&gp->viewRect, dh, dv, updateRgn);
- OffsetRect(&gp->destRect, dh, dv);
- SectRect(&gp->viewRect, &(*updateRgn)->rgnBBox, &r);
- offRect = r;
- OffsetRect(&offRect, gp->viewRect.left - gp->destRect.left, gp->viewRect.top - gp->destRect.top);
- SetPort (CAMLGraph);
- copy_bits (&CAMLOffScreen->portBits, &CAMLGraph->portBits, &offRect, &r, srcCopy, nil);
- DisposeRgn(updateRgn);
- AdjustScrollBars(doc);
- }
-
- void DoGrowWindow(WindowPtr window, EventRecord *event)
- {
- Rect r, *destRect;
- long newSize;
- short bound;
-
- if (((WindowPeek) window)->windowKind == userKind) {
- if (isGraphWindow(window)) {
- r.right = gp->destRect.right - gp->destRect.left + SBWIDTH;
- r.bottom = gp->destRect.bottom - gp->destRect.top + SBWIDTH;
- } else {
- r.right = qd.screenBits.bounds.right;
- r.bottom = qd.screenBits.bounds.bottom;
- }
- r.left = WMINSIZE;
- r.top = WMINSIZE;
- if (newSize = GrowWindow(window, event->where, &r)) {
- SetPort(window);
- InvalRect(&window->portRect); /* on redessine tout ! */
- SizeWindow(window, LoWord(newSize), HiWord(newSize), true); /* la fenetre sera dessinee via un updateEvt */
- if (isGraphWindow(window)) {
- destRect = &((graphPeek) CAMLOffScreen)->destRect;
- bound = window->portRect.right - SBWIDTH + 1;
- if (bound > destRect->right)
- OffsetRect(destRect, bound - destRect->right, 0);
- bound = window->portRect.bottom - SBWIDTH + 1;
- if (bound > destRect->bottom)
- OffsetRect(destRect, 0, bound - destRect->bottom);
- ClipRect(&CAMLGraph->portRect);
- }
- FixViewRect(window);
- (*((dataPeek) window)->vSBar)->contrlVis = inVisible; /* pour que AdjustScrollBars ne redessine pas */
- (*((dataPeek) window)->hSBar)->contrlVis = inVisible; /* les controles, ce sera fait par UpdtControls */
- AdjustScrollBars((dataPeek) window);
- FixScrollBar((dataPeek) window); /* remet les controles a l'etat visible */
- }
- }
- }
-
- Boolean DoNew(short index, Rect *boundsRect)
- {
- Ptr storage;
- WindowPtr window;
- Str255 title;
- dataPeek doc;
- Boolean goAwayFlag;
- Rect r;
-
- storage = NewPtr(sizeof(dataRecord));
- if (storage != nil) {
- GetIndString(title, rWTitle, index);
- goAwayFlag = ((index == tHistory) || (index == tGraphic));
- if (color_qd && index == tGraphic){
- window = (WindowPtr) NewCWindow (storage, boundsRect, title, false, zoomDocProc, nil, goAwayFlag, 0);
- }else{
- window = NewWindow(storage, boundsRect, title, false, zoomDocProc, nil, goAwayFlag, 0);
- }
- doc = (dataPeek) window;
- SetRect(&r, 0, 0, 16, 50);
- doc->vSBar = NewControl(window, &r, "\p", false, 0, 0, 0, scrollBarProc, 0);
- doc->hSBar = NewControl(window, &r, "\p", false, 0, 0, 0, scrollBarProc, 0);
- doc->type = index;
- doc->h = nil;
- doc->prevWindow = nil;
- doc->nextWindow = WList;
- if (WList != nil)
- WList->prevWindow = doc;
- WList = doc;
- SetPort(window);
- SetOrigin(0,0);
- TextFont(monaco); /* la largeur des caractères est constante dans cette font */
- TextSize(FONTSIZE);
- FixScrollBar(doc);
- (*doc->vSBar)->contrlVis = inVisible; /* ShowWindow genere un updateEvt, qui prendra en charge le */
- (*doc->hSBar)->contrlVis = inVisible; /* remplissage de la nouvelle fenetre; il faut forcer les */
- if (index != tHistory) /* scroll bars a etre redessinees par DoActivate. */
- ShowWindow(window);
- return true;
- }
- return false;
- }
-
- void OpenOutput(short windowType)
- {
- Rect r;
- dataPeek doc;
- FontInfo info;
- textPtr t;
- Handle temp;
-
- r = (windowType == tOutput) ? Pref->PrWindowRect : Pref->HiWindowRect;
- if (DoNew(windowType, &r)) {
- doc = WList;
- doc->h = (long) NewHandle(sizeof(textRecord));
- if (doc->h != nil) {
- temp = NewHandle(TEBLOCK);
- (*((textHandle) doc->h))->hText = temp;
- if (temp != nil) {
- GetFontInfo(&info);
- t = *((textHandle) doc->h); /* attention aux routines qui deplacent ou purgent la memoire ! */
- t->lineHeight = info.ascent + info.descent + info.leading;
- t->ascent = info.ascent;
- t->width = info.widMax;
- FixViewRect((WindowPtr) doc);
- t->destRect.top = t->viewRect.top;
- t->destRect.right = (t->destRect.left = t->viewRect.left);
- t->last_line_length = 0;
- t->selStart = 0;
- t->selEnd = 0;
- t->teLength = 0;
- t->firstLine = 0;
- t->nLines = 1;
- AdjustScrollBars(doc);
- if (windowType == tOutput) {
- CAMLPrinter = (WindowPtr) doc;
- t->buffer = nil;
- return;
- } else {
- temp = NewHandle(BUFBLOCK);
- (*((textHandle) doc->h))->buffer = (bufHandle) temp;
- if (temp != nil) {
- (*((bufHandle) temp))[0] = 2;
- (*((bufHandle) temp))[1] = 2;
- (*((bufHandle) temp))[2] = 0;
- CAMLHistory = (WindowPtr) doc;
- if (Pref->ShowHist)
- ShowWindow(CAMLHistory);
- return;
- }
- }
- }
- }
- }
- Error(eNoMemory);
- Pref->SavePos = false;
- Quit();
- }
-
- void OpenInput(void)
- {
- dataPeek doc;
- Rect destRect;
- TEHandle te;
-
- if (DoNew(tInput, &Pref->LiWindowRect)) {
- doc = WList;
- CAMLInput = (WindowPtr) doc;
- destRect = ((WindowPtr) doc)->portRect;
- doc->h = (long) TENew(&destRect, &destRect);
- if (doc->h != nil) {
- te = (TEHandle) doc->h;
- TEAutoView(true, te);
- (*te)->crOnly = WRAP;
- oldClikLoop = (*te)->clikLoop;
- (*te)->clikLoop = (ClikLoopProcPtr) AsmClikLoop;
- FixViewRect((WindowPtr) doc);
- destRect = (*te)->viewRect;
- destRect.right = destRect.left + TEWSIZE * CHARWIDTH;
- (*te)->destRect = destRect;
- AdjustScrollBars(doc);
- return;
- } else
- Error(eNoTextEdit);
- } else
- Error(eNoMemory);
- Pref->SavePos = false;
- Quit();
- }
-
- Boolean OpenGraph(void)
- {
- dataPeek doc;
- Rect r;
-
- if (DoNew(tGraphic, &Pref->GrWindowRect)) {
- graph_event_head = graph_event_tail = 0;
- CAMLGraph = (WindowPtr) (doc = WList);
- doc->h = (long) NewPtr(sizeof(graphRecord));
- if (doc->h != nil) {
- FixViewRect(CAMLGraph);
- CAMLOffScreen = (GrafPtr) doc->h;
- r = gp->viewRect;
- r.right = r.left + SIZEX;
- r.bottom = r.top + SIZEY;
- if (color_qd){
- GDHandle the_max_device, old_device;
- CTabHandle color_map;
- Rect wide_open = {-32000, -32000, 32000, 32000};
- CGrafPtr color_off_screen = (CGrafPtr) CAMLOffScreen;
- long off_row_bytes;
-
- the_max_device = GetMaxDevice (&wide_open);
- old_device = GetGDevice ();
- CAMLGDevice = NewGDevice (0, 0);
- max_depth = (*(*the_max_device)->gdPMap)->pixelSize;
- **(*CAMLGDevice)->gdPMap = **(*the_max_device)->gdPMap;
- off_row_bytes = (max_depth * (r.right - r.left) + 31) / 32 * 4;
- (*(*CAMLGDevice)->gdPMap)->rowBytes = off_row_bytes + 0x8000;
- (*(*CAMLGDevice)->gdPMap)->baseAddr = NewPtr (off_row_bytes * (long) (r.bottom - r.top));
- if ((*(*CAMLGDevice)->gdPMap)->baseAddr == nil){
- DoClose (CAMLGraph);
- return false;
- }
- (*(*CAMLGDevice)->gdPMap)->bounds = r;
- color_map = (*(*the_max_device)->gdPMap)->pmTable;
- if (HandToHand ((Handle *) &color_map) != noErr){
- DoClose (CAMLGraph);
- return false;
- }
- (*(*CAMLGDevice)->gdPMap)->pmTable = color_map;
- SetGDevice (CAMLGDevice);
- MakeITable (nil, nil, (*the_max_device)->gdResPref);
- if (QDError () != noErr){
- SetGDevice (old_device);
- DoClose (CAMLGraph);
- return false;
- }
- OpenCPort (color_off_screen);
- SetGDevice (old_device);
- SetPalette (CAMLGraph, nil, 1);
- }else{
- max_depth = 1;
- OpenPort(CAMLOffScreen);
- CAMLOffScreen->portBits.rowBytes = (r.right - r.left + 31) / 32 * 4;
- CAMLOffScreen->portBits.baseAddr = NewPtr(CAMLOffScreen->portBits.rowBytes * (long) (r.bottom - r.top));
- CAMLOffScreen->portBits.bounds = r;
- if (CAMLOffScreen->portBits.baseAddr == nil){
- DoClose (CAMLGraph);
- return false;
- }
- }
- CAMLOffScreen->portRect = r;
- EraseRect(&r);
- r.bottom = gp->viewRect.bottom;
- r.top = r.bottom - SIZEY;
- gp->destRect = r;
- gp->destRectZoom = r;
- r = qd.screenBits.bounds;
- r.top += GetMBarHeight() + SBWIDTH;
- InsetRect(&r, MARGINDRAG, MARGINDRAG);
- if (r.right - r.left > SIZEX + SBWIDTH - 1)
- r.left = r.right - SIZEX - SBWIDTH + 1;
- if (r.bottom - r.top > SIZEY + SBWIDTH - 1)
- r.top = r.bottom - SIZEY - SBWIDTH + 1;
- (*((WStateDataHandle) ((WindowPeek) CAMLGraph)->dataHandle))->stdState = r;
- TextFont(monaco);
- TextSize(FONTSIZE);
- SetPort(CAMLGraph);
- AdjustScrollBars(doc);
- EnableItem(GetMHandle(mCaml), iShowGraph);
- SelectWindow(CAMLGraph);
- return true;
- }
- DoClose(CAMLGraph);
- }
- return false;
- }
-
- void DoClose(WindowPtr window)
- {
- dataPeek doc;
-
- doc = (dataPeek) window;
- if (doc->h != nil)
- switch (WindowType(window)) {
- case tInput:
- TEDispose((TEHandle) doc->h);
- break;
- case tOutput:
- case tHistory:
- if ((*((textHandle) doc->h))->hText != nil)
- DisposeHandle((*((textHandle) doc->h))->hText);
- if ((window == CAMLHistory) && ((*((textHandle) doc->h))->buffer != nil))
- DisposeHandle((Handle) (*((textHandle) doc->h))->buffer);
- DisposeHandle((Handle) doc->h);
- break;
- case tGraphic:
- GetWindowPos(CAMLGraph, &Pref->GrWindowRect);
- if (color_qd){
- if ((*((CGrafPtr) CAMLOffScreen)->portPixMap)->baseAddr != nil)
- DisposePtr ((*((CGrafPtr) CAMLOffScreen)->portPixMap)->baseAddr);
- CloseCPort ((CGrafPtr) CAMLOffScreen);
- DisposGDevice (CAMLGDevice);
- CAMLGDevice = nil;
- }else{
- if (CAMLOffScreen->portBits.baseAddr != nil)
- DisposePtr(CAMLOffScreen->portBits.baseAddr);
- ClosePort(CAMLOffScreen);
- }
- DisposePtr((Ptr) CAMLOffScreen);
- DisableItem(GetMHandle(mCaml), iShowGraph);
- CAMLGraph = nil;
- CAMLOffScreen = nil;
- }
- CloseWindow(window);
- if (doc->prevWindow == nil)
- WList = doc->nextWindow;
- else
- (doc->prevWindow)->nextWindow = doc->nextWindow;
- if (doc->nextWindow != nil)
- (doc->nextWindow)->prevWindow = doc->prevWindow;
- DisposePtr((Ptr) window);
- }
-
- pascal ProcPtr GetOldClikLoop(void)
- {
- return (ProcPtr) oldClikLoop;
- }
-
- pascal void ClikLoop(void)
- {
- WindowPtr window;
- RgnHandle clipRgn;
-
- window = FrontWindow();
- clipRgn = NewRgn();
- GetClip(clipRgn);
- ClipRect(&window->portRect);
- AdjustScrollBars((dataPeek) window);
- SetClip(clipRgn);
- DisposeRgn(clipRgn);
- }
-
- void FixViewRect(WindowPtr window)
- {
- Rect viewRect;
- long h;
- short height;
-
- if (isGraphWindow(window)) {
- viewRect = window->portRect;
- viewRect.right -= SBWIDTH - 1;
- viewRect.bottom -= SBWIDTH - 1;
- ((graphPeek) ((dataPeek) window)->h)->viewRect = viewRect;
- } else {
- viewRect.top = TXTMARG;
- viewRect.left = TXTMARG;
- viewRect.bottom = window->portRect.bottom - SBWIDTH + 1 - TXTMARG;
- viewRect.right = window->portRect.right - SBWIDTH + 1;
- if (isTEWindow(window))
- viewRect.right -= 2;
- h = ((dataPeek) window)->h;
- height = (isTEWindow(window)) ? (*((TEHandle) h))->lineHeight : (*((textHandle) h))->lineHeight;
- viewRect.bottom = viewRect.top + ((viewRect.bottom - viewRect.top) / height) * height;
- if (isTEWindow(window))
- (*((TEHandle) h))->viewRect = viewRect;
- else
- (*((textHandle) h))->viewRect = viewRect;
- }
- }
-
- void FixScrollBar(dataPeek doc)
- {
- Rect r;
-
- r = ((WindowPtr) doc)->portRect;
- (*doc->vSBar)->contrlVis = inVisible;
- (*doc->hSBar)->contrlVis = inVisible;
- SizeControl(doc->vSBar, SBWIDTH, r.bottom - r.top - SBWIDTH + 3);
- MoveControl(doc->vSBar, r.right - SBWIDTH + 1, -1);
- SizeControl(doc->hSBar, r.right - r.left - SBWIDTH + 3, SBWIDTH);
- MoveControl(doc->hSBar, -1, r.bottom - SBWIDTH + 1);
- (*doc->vSBar)->contrlVis = Visible;
- (*doc->hSBar)->contrlVis = Visible;
- }
-
- void Error(short error)
- {
- Str255 message;
- Handle h;
-
- SetCursor(&qd.arrow);
- GetIndString(message, rErrorList, error);
- ParamText(message, "\p", "\p", "\p");
- h = GetResource('ALRT', rErrorAlert);
- CenterRect((Rect *) *h);
- StopAlert(rErrorAlert, nil);
- ReleaseResource(h);
- }
-